package com.boss.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import com.boss.example.entity.dto.FunsDTO;
import com.boss.example.entity.vo.FunsVO;
import com.boss.example.entity.vo.CommonResult;
import com.boss.example.service.FunsService;

/**
 * @author Hu_liantian
 * @date 2020/8/6
 */
@Slf4j
@RestController
@RequestMapping("/fun")
public class FunsController {

    @Resource
    private FunsService functionService;

    @PostMapping("/update")
    public CommonResult updateFunction(FunsVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO != null){
            int result = functionService.updateFunction(funsDTO);
            if(result > 0){
                return new CommonResult(200, "插入成功", result);
            }
        }
        return new CommonResult(400, "插入失败", null);
    }

    @PostMapping("/insert")
    public CommonResult insertFunction(@RequestBody FunsVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO != null){
            int result = functionService.insertFunction(funsDTO);
            if(result > 0){
                return new CommonResult(200, "插入成功", result);
            }
        }
        return new CommonResult(400, "插入失败", null);
    }

    @PostMapping("/delete")
    public CommonResult deleteFunction(@RequestBody FunsVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO.getFunId() != null){
            int result = functionService.deleteFunction(funsDTO.getMenuId());
            if(result > 0){
                return new CommonResult(200, "删除成功", result);
            }
        }
        return new CommonResult(400, "删除失败", null);
    }

    @PostMapping("/get")
    public CommonResult getFunctionByMenuId(@RequestBody FunsVO funVO) {
        FunsDTO funsDTO = new FunsDTO();
        BeanUtils.copyProperties(funVO,funsDTO);
        if(funsDTO.getFunId() != null){
            List<FunsDTO> funDTOList = functionService.getFunctionByMenuId(funsDTO.getMenuId());
            if(funDTOList != null){
                return new CommonResult(200, "查找成功", funDTOList);
            }
        }
        return new CommonResult(400, "查找失败", null);
    }

}
